package com.fanli.scheduler.controller;

import com.fanli.scheduler.bean.HiveEsTemplate;
import com.fanli.scheduler.bean.Result;
import com.fanli.scheduler.bean.TemplateBean;
import com.fanli.scheduler.entity.EsTaskExtra;
import com.fanli.scheduler.service.ESService;
import com.fanli.scheduler.service.TaskConfigService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xu.liu on 2016/9/7.
 */
@Controller
@RequestMapping("/es")
public class ESTransferTaskController {

    @Autowired
    ESService esService;
    @Autowired
    private TaskConfigService taskConfigService;
    @RequestMapping(value = "/createTemplate",method = RequestMethod.POST)
    @ResponseBody
    public Result createTemplate(@RequestBody TemplateBean templateBean){
        String templateName = templateBean.getTemplateName();
        String templateIndex = templateBean.getTemplateIndex();
        String templateData = templateBean.getTemplateData().toString();
        String templateMapName = templateBean.getTemplateMapName();
        System.err.println("templateName --> "+templateName);
        System.err.println("templateIndex --> "+templateIndex);
        System.err.println("templateData --> "+templateData);
        System.err.println("templateDatabaseTable --> "+ templateBean.getTemplateDatabaseTable());
        System.err.println("--------------------------------------------------------------------------------------------------------------");
        ObjectMapper mapper = new ObjectMapper();
        Result result = new Result();
        try {
            List<HiveEsTemplate> datas  = mapper.readValue(templateData, mapper.getTypeFactory().constructParametricType(ArrayList.class, HiveEsTemplate.class));
            if (CollectionUtils.isNotEmpty(datas)){
                boolean re=esService.createEsTemplate(templateName,templateIndex,templateMapName,datas);
                result.setIsSuccess(re);
            }else{
                result.setIsSuccess(false);
            }
        } catch (IOException e) {
            result.setIsSuccess(false);
            e.printStackTrace();
        }
        return result;
    }
    @RequestMapping(value = "/createTaskConf",method = RequestMethod.POST)
    @ResponseBody
    public Result createTaskConf(@RequestParam String taskConf){
        System.err.println("taskConf --> "+taskConf);
        System.err.println("--------------------------------------------------------------------------------------------------------------");
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/createTaskLoadConf",method = RequestMethod.POST)
    @ResponseBody
    public Result createTaskLoadConf(@RequestParam String templateName,@RequestParam String templateData,@RequestParam String cycle,
                                     @RequestParam String tansferType,
                                     @RequestParam String incrField){
        System.err.println("templateName --> "+templateName);
        System.err.println("cycle --> "+cycle);
        System.err.println("templateData --> "+templateData);
        System.err.println("tansferType --> "+tansferType);
        System.err.println("incrField --> "+incrField);
        System.err.println("--------------------------------------------------------------------------------------------------------------");
        return null;
    }

    @RequestMapping("/addExtEsConf")
    @ResponseBody
    public Result addExtEsConf(@RequestBody EsTaskExtra esTaskExtra){
        Result result = new Result();
        Date date = new Date();
        esTaskExtra.setAddTime(date);
        esTaskExtra.setUpdateTime(date);
        boolean isSuccess = taskConfigService.insertEsTaskExtra(esTaskExtra) > 0;
        result.setIsSuccess(isSuccess);
        return result;
    }
}
